var panel;
function setPanel(p){
	panel=p;
	if(panel.getAttribute("externaljs")){
		var external=panel.getAttribute("externaljs").split(";");
		for(var j=0;j<external.length;j++){
			if(external[j]!=""){
				scriptLoader.loadScript(external[j]);
			}
		}
	}
	panel.modals=new Object();
	var divs=panel.getElementsByTagName("DIV");
	for(var i=0;i<divs.length;i++){
		if(divs[i].className=="grid"){
			if(divs[i].id=="mainGrid"){
				panel.grid=divs[i];
			}
		}
		if(divs[i].className=="modal"){
			panel.modals[divs[i].id]=divs[i];
		}
	}
	panel.add=function(){
		openModal(this.modals["edit"]);
		panel.modalAction="add";
	}
	panel.remove=function(){
		if(this.grid.selectElement){
			var vars=this.getSendData(this.grid.selectedElement);
			sendVarsAndDo(ROOT_PATH+ACTION_PATH+"?doAction="+panel.getAttribute("action")+"&action=remove",vars,function(){panel.refreshList()});
			closeOpenedModal();
		}
	}
	panel.modify=function(){
		if(this.grid.selectElement){
			var els=this.getNamedValues(this.grid.selectedElement);
			openModal(this.modals["edit"]);
			panel.modalAction="modify";
			var fields=this.getFields(openedModal);
			for(var i=0;i<fields.length;i++){
				if(els[fields[i].name]){
					if(fields[i].tagName=="INPUT"){
						fields[i].value=els[fields[i].name];
					}
					if(fields[i].tagName=="SELECT"){
						var val=els[fields[i].name];
						for(var o=0;o<fields[i].options.length;o++){
							if(fields[i].options[o].value==val){
								fields[i].options[o].selected=true;
							}
						}
					}
				}
			}
		}
	}
	panel.confirmEditModal=function(){
		var vars=this.getModalData();
		sendVarsAndDo(ROOT_PATH+ACTION_PATH+"?doAction="+panel.getAttribute("action")+"&action="+panel.modalAction,vars,function(){panel.refreshList()});
		closeOpenedModal();
	}
	panel.getModalData=function(){
		return this.getSendData(openedModal);
	}
	panel.refreshList=function(){
		var action=this.getAttribute("action");
		var url=ROOT_PATH+ACTION_PATH+"?doAction="+action+"&action=list";
		var loader=new xmlLoader();
		loader.onload=function(){
			if(panel.grid){
				panel.grid.innerHTML=this.textLoaded;
			}
		}
		loader.load(url);
	}
	panel.getData=function(el){
		var data=new Array();
		var els=el.getElementsByTagName("*");
		for(var i=0;i<els.length;i++){
			if(els[i].tagName=="INPUT"){
				data.push({ name:[els[i].name],value:els[i].value});
			}
			if(els[i].tagName=="SELECT"){
				data.push({ name:[els[i].name],value:els[i].options[els[i].selectedIndex].value });
			}
		}
		return data;
	}
	panel.getSendData=function(el){
		var vars="";
		var els=this.getData(el);
		for(var i=0;i<els.length;i++){
			vars+=els[i].name+"="+escape(els[i].value)+"&";
		}
		return vars;
	}
	
	panel.getFields=function(el){
		var fields=new Array();
		var els=el.getElementsByTagName("*");
		for(var i=0;i<els.length;i++){
			if(els[i].tagName=="INPUT" || els[i].tagName=="SELECT"){
				fields.push(els[i]);
			}
		}
		return fields;
	}
	
	panel.getNamedValues=function(el){
		var values={};
		var els=this.getData(el);
		for(var i=0;i<els.length;i++){
			values[els[i].name]=els[i].value;
		}
		return values;
	}
	
	var onload=panel.getAttribute("onload");
	setTimeout(onload,200);
}
